home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / DJLSR111.ZIP / libsrc / c / gen / ctime.c < prev    next >
C/C++ Source or Header  |  1993-06-28  |  36KB  |  1,379 lines

  1. /* This is file CTIME.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1993 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1987, 1989 Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * This code is derived from software contributed to Berkeley by
  12.  * Arthur David Olson of the National Cancer Institute.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. #if defined(LIBC_SCCS) && !defined(lint)
  30. static char sccsid[] = "@(#)ctime.c    5.23 (Berkeley) 6/22/90";
  31. #endif /* LIBC_SCCS and not lint */
  32.  
  33. /*
  34. ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
  35. ** POSIX-style TZ environment variable handling from Guy Harris
  36. ** (guy@auspex.com).
  37. */
  38.  
  39. /*LINTLIBRARY*/
  40.  
  41. #include <sys/param.h>
  42. #include <fcntl.h>
  43. #include <time.h>
  44. #include <tzfile.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include <stdio.h>
  48.  
  49. #ifdef __STDC__
  50. #include <stdlib.h>
  51.  
  52. #define P(s)        s
  53. #define alloc_size_t    size_t
  54. #define qsort_size_t    size_t
  55. #define fread_size_t    size_t
  56. #define fwrite_size_t    size_t
  57.  
  58. #else /* !defined __STDC__ */
  59.  
  60. #define P(s)        ()
  61. #define const
  62. #define volatile
  63.  
  64. typedef char *        genericptr_t;
  65. typedef unsigned    alloc_size_t;
  66. typedef int        qsort_size_t;
  67. typedef int        fread_size_t;
  68. typedef int        fwrite_size_t;
  69.  
  70. extern char *    calloc();
  71. extern char *    malloc();
  72. extern char *    realloc();
  73. extern char *    getenv();
  74.  
  75. #endif /* !defined __STDC__ */
  76.  
  77. extern unsigned long    time();
  78.  
  79. #define FILENAME_MAX    MAXPATHLEN
  80. #define ACCESS_MODE    O_RDONLY
  81. #define OPEN_MODE    O_RDONLY
  82.  
  83. #ifndef WILDABBR
  84. /*
  85. ** Someone might make incorrect use of a time zone abbreviation:
  86. **    1.    They might reference tzname[0] before calling tzset (explicitly
  87. **         or implicitly).
  88. **    2.    They might reference tzname[1] before calling tzset (explicitly
  89. **         or implicitly).
  90. **    3.    They might reference tzname[1] after setting to a time zone
  91. **        in which Daylight Saving Time is never observed.
  92. **    4.    They might reference tzname[0] after setting to a time zone
  93. **        in which Standard Time is never observed.
  94. **    5.    They might reference tm.TM_ZONE after calling offtime.
  95. ** What's best to do in the above cases is open to debate;
  96. ** for now, we just set things up so that in any of the five cases
  97. ** WILDABBR is used.  Another possibility:  initialize tzname[0] to the
  98. ** string "tzname[0] used before set", and similarly for the other cases.
  99. ** And another:  initialize tzname[0] to "ERA", with an explanation in the
  100. ** manual page of what this "time zone abbreviation" means (doing this so
  101. ** that tzname[0] has the "normal" length of three characters).
  102. */
  103. #define WILDABBR    "   "
  104. #endif /* !defined WILDABBR */
  105.  
  106. #ifndef TRUE
  107. #define TRUE        1
  108. #define FALSE        0
  109. #endif /* !defined TRUE */
  110.  
  111. static const char GMT[] = "GMT";
  112.  
  113. struct ttinfo {                /* time type information */
  114.     long        tt_gmtoff;    /* GMT offset in seconds */
  115.     int        tt_isdst;    /* used to set tm_isdst */
  116.     int        tt_abbrind;    /* abbreviation list index */
  117.     int        tt_ttisstd;    /* TRUE if transition is std time */
  118. };
  119.  
  120. struct lsinfo {                /* leap second information */
  121.     time_t        ls_trans;    /* transition time */
  122.     long        ls_corr;    /* correction to apply */
  123. };
  124.  
  125. struct state {
  126.     int        leapcnt;
  127.     int        timecnt;
  128.     int        typecnt;
  129.     int        charcnt;
  130.     time_t        ats[TZ_MAX_TIMES];
  131.     unsigned char    types[TZ_MAX_TIMES];
  132.     struct ttinfo    ttis[TZ_MAX_TYPES];
  133.     char        chars[(TZ_MAX_CHARS + 1 > sizeof GMT) ?
  134.                 TZ_MAX_CHARS + 1 : sizeof GMT];
  135.     struct lsinfo    lsis[TZ_MAX_LEAPS];
  136. };
  137.  
  138. struct rule {
  139.     int        r_type;        /* type of rule--see below */
  140.     int        r_day;        /* day number of rule */
  141.     int        r_week;        /* week number of rule */
  142.     int        r_mon;        /* month number of rule */
  143.     long        r_time;        /* transition time of rule */
  144. };
  145.  
  146. #define    JULIAN_DAY        0    /* Jn - Julian day */
  147. #define    DAY_OF_YEAR        1    /* n - day of year */
  148. #define    MONTH_NTH_DAY_OF_WEEK    2    /* Mm.n.d - month, week, day of week */
  149.  
  150. /*
  151. ** Prototypes for static functions.
  152. */
  153.  
  154. static long        detzcode P((const char * codep));
  155. static const char *    getzname P((const char * strp));
  156. static const char *    getnum P((const char * strp, int * nump, int min,
  157.                 int max));
  158. static const char *    getsecs P((const char * strp, long * secsp));
  159. static const char *    getoffset P((const char * strp, long * offsetp));
  160. static const char *    getrule P((const char * strp, struct rule * rulep));
  161. static void        gmtload P((struct state * sp));
  162. static void        gmtsub P((const time_t * timep, long offset,
  163.                 struct tm * tmp));
  164. static void        localsub P((const time_t * timep, long offset,
  165.                 struct tm * tmp));
  166. static void        normalize P((int * tensptr, int * unitsptr, int base));
  167. static void        settzname P((void));
  168. static time_t        time1 P((struct tm * tmp, void (* funcp)(),
  169.                 long offset));
  170. static time_t        time2 P((struct tm *tmp, void (* funcp)(),
  171.                 long offset, int * okayp));
  172. static void        timesub P((const time_t * timep, long offset,
  173.                 const struct state * sp, struct tm * tmp));
  174. static int        tmcomp P((const struct tm * atmp,
  175.                 const struct tm * btmp));
  176. static time_t        transtime P((time_t janfirst, int year,
  177.                 const struct rule * rulep, long offset));
  178. static int        tzload P((const char * name, struct state * sp));
  179. static int        tzparse P((const char * name, struct state * sp,
  180.                 int lastditch));
  181.  
  182. #ifdef ALL_STATE
  183. static struct state *    lclptr;
  184. static struct state *    gmtptr;
  185. #endif /* defined ALL_STATE */
  186.  
  187. #ifndef ALL_STATE
  188. static struct state    lclmem;
  189. static struct state    gmtmem;
  190. #define lclptr        (&lclmem)
  191. #define gmtptr        (&gmtmem)
  192. #endif /* State Farm */
  193.  
  194. static int        lcl_is_set;
  195. static int        gmt_is_set;
  196.  
  197. char *            tzname[2] = {
  198.     WILDABBR,
  199.     WILDABBR
  200. };
  201.  
  202. #ifdef USG_COMPAT
  203. time_t            timezone = 0;
  204. int            daylight = 0;
  205. #endif /* defined USG_COMPAT */
  206.  
  207. #ifdef ALTZONE
  208. time_t            altzone = 0;
  209. #endif /* defined ALTZONE */
  210.  
  211. static long
  212. detzcode(codep)
  213. const char * const    codep;
  214. {
  215.     register long    result;
  216.     register int    i;
  217.  
  218.     result = 0;
  219.     for (i = 0; i < 4; ++i)
  220.         result = (result << 8) | (codep[i] & 0xff);
  221.     return result;
  222. }
  223.  
  224. static void
  225. settzname()
  226. {
  227.     register const struct state * const    sp = lclptr;
  228.     register int                i;
  229.  
  230.     tzname[0] = WILDABBR;
  231.     tzname[1] = WILDABBR;
  232. #ifdef USG_COMPAT
  233.     daylight = 0;
  234.     timezone = 0;
  235. #endif /* defined USG_COMPAT */
  236. #ifdef ALTZONE
  237.     altzone = 0;
  238. #endif /* defined ALTZONE */
  239. #ifdef ALL_STATE
  240.     if (sp == NULL) {
  241.         tzname[0] = tzname[1] = GMT;
  242.         return;
  243.     }
  244. #endif /* defined ALL_STATE */
  245.     for (i = 0; i < sp->typecnt; ++i) {
  246.         register const struct ttinfo * const    ttisp = &sp->ttis[i];
  247.  
  248.         tzname[ttisp->tt_isdst] =
  249.             (char *) &sp->chars[ttisp->tt_abbrind];
  250. #ifdef USG_COMPAT
  251.         if (ttisp->tt_isdst)
  252.             daylight = 1;
  253.         if (i == 0 || !ttisp->tt_isdst)
  254.             timezone = -(ttisp->tt_gmtoff);
  255. #endif /* defined USG_COMPAT */
  256. #ifdef ALTZONE
  257.         if (i == 0 || ttisp->tt_isdst)
  258.             altzone = -(ttisp->tt_gmtoff);
  259. #endif /* defined ALTZONE */
  260.     }
  261.     /*
  262.     ** And to get the latest zone names into tzname. . .
  263.     */
  264.     for (i = 0; i < sp->timecnt; ++i) {
  265.         register const struct ttinfo * const    ttisp =
  266.                             &sp->ttis[sp->types[i]];
  267.  
  268.         tzname[ttisp->tt_isdst] =
  269.             (char *) &sp->chars[ttisp->tt_abbrind];
  270.     }
  271. }
  272.  
  273. static int
  274. tzload(name, sp)
  275. register const char *        name;
  276. register struct state * const    sp;
  277. {
  278.     register const char *    p;
  279.     register int        i;
  280.     register int        fid;
  281.  
  282.     if (name == NULL && (name = TZDEFAULT) == NULL)
  283.         return -1;
  284.     {
  285.         char        fullname[FILENAME_MAX + 1];
  286.  
  287.         if (name[0] == ':')
  288.             ++name;
  289.         if (name[0] != '/') {
  290.             if ((p = TZDIR) == NULL)
  291.                 return -1;
  292.             if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
  293.                 return -1;
  294.             (void) strcpy(fullname, p);
  295.             (void) strcat(fullname, "/");
  296.             (void) strcat(fullname, name);
  297.             name = fullname;
  298.         }
  299.         if ((fid = open(name, OPEN_MODE)) == -1)
  300.             return -1;
  301.     }
  302.     {
  303.         register const struct tzhead *    tzhp;
  304.         char                buf[sizeof *sp + sizeof *tzhp];
  305.         int                ttisstdcnt;
  306.  
  307.         i = read(fid, buf, sizeof buf);
  308.         if (close(fid) != 0 || i < sizeof *tzhp)
  309.             return -1;
  310.         tzhp = (struct tzhead *) buf;
  311.         ttisstdcnt = (int) detzcode(tzhp->tzh_ttisstdcnt);
  312.         sp->leapcnt = (int) detzcode(tzhp->tzh_leapcnt);
  313.         sp->timecnt = (int) detzcode(tzhp->tzh_timecnt);
  314.         sp->typecnt = (int) detzcode(tzhp->tzh_typecnt);
  315.         sp->charcnt = (int) detzcode(tzhp->tzh_charcnt);
  316.         if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
  317.             sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
  318.             sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
  319.             sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
  320.             (ttisstdcnt != sp->typecnt && ttisstdcnt != 0))
  321.                 return -1;
  322.         if (i < sizeof *tzhp +
  323.             sp->timecnt * (4 + sizeof (char)) +
  324.             sp->typecnt * (4 + 2 * sizeof (char)) +
  325.             sp->charcnt * sizeof (char) +
  326.             sp->leapcnt * 2 * 4 +
  327.             ttisstdcnt * sizeof (char))
  328.                 return -1;
  329.         p = buf + sizeof *tzhp;
  330.         for (i = 0; i < sp->timecnt; ++i) {
  331.             sp->ats[i] = detzcode(p);
  332.             p += 4;
  333.         }
  334.         for (i = 0; i < sp->timecnt; ++i) {
  335.             sp->types[i] = (unsigned char) *p++;
  336.             if (sp->types[i] >= sp->typecnt)
  337.                 return -1;
  338.         }
  339.         for (i = 0; i < sp->typecnt; ++i) {
  340.             register struct ttinfo *    ttisp;
  341.  
  342.             ttisp = &sp->ttis[i];
  343.             ttisp->tt_gmtoff = detzcode(p);
  344.             p += 4;
  345.             ttisp->tt_isdst = (unsigned char) *p++;
  346.             if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
  347.                 return -1;
  348.             ttisp->tt_abbrind = (unsigned char) *p++;
  349.             if (ttisp->tt_abbrind < 0 ||
  350.                 ttisp->tt_abbrind > sp->charcnt)
  351.                     return -1;
  352.         }
  353.         for (i = 0; i < sp->charcnt; ++i)
  354.             sp->chars[i] = *p++;
  355.         sp->chars[i] = '\0';    /* ensure '\0' at end */
  356.         for (i = 0; i < sp->leapcnt; ++i) {
  357.             register struct lsinfo *    lsisp;
  358.  
  359.             lsisp = &sp->lsis[i];
  360.             lsisp->ls_trans = detzcode(p);
  361.             p += 4;
  362.             lsisp->ls_corr = detzcode(p);
  363.             p += 4;
  364.         }
  365.         for (i = 0; i < sp->typecnt; ++i) {
  366.             register struct ttinfo *    ttisp;
  367.  
  368.             ttisp = &sp->ttis[i];
  369.             if (ttisstdcnt == 0)
  370.                 ttisp->tt_ttisstd = FALSE;
  371.             else {
  372.                 ttisp->tt_ttisstd = *p++;
  373.                 if (ttisp->tt_ttisstd != TRUE &&
  374.                     ttisp->tt_ttisstd != FALSE)
  375.                         return -1;
  376.             }
  377.         }
  378.     }
  379.     return 0;
  380. }
  381.  
  382. static const int    mon_lengths[2][MONSPERYEAR] = {
  383.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
  384.     31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  385. };
  386.  
  387. static const int    year_lengths[2] = {
  388.     DAYSPERNYEAR, DAYSPERLYEAR
  389. };
  390.  
  391. /*
  392. ** Given a pointer into a time zone string, scan until a character that is not
  393. ** a valid character in a zone name is found.  Return a pointer to that
  394. ** character.
  395. */
  396.  
  397. static const char *
  398. getzname(strp)
  399. register const char *    strp;
  400. {
  401.     register char    c;
  402.  
  403.     while ((c = *strp) != '\0' && !isdigit(c) && c != ',' && c != '-' &&
  404.         c != '+')
  405.             ++strp;
  406.     return strp;
  407. }
  408.  
  409. /*
  410. ** Given a pointer into a time zone string, extract a number from that string.
  411. ** Check that the number is within a specified range; if it is not, return
  412. ** NULL.
  413. ** Otherwise, return a pointer to the first character not part of the number.
  414. */
  415.  
  416. static const char *
  417. getnum(strp, nump, min, max)
  418. register const char *    strp;
  419. int * const        nump;
  420. const int        min;
  421. const int        max;
  422. {
  423.     register char    c;
  424.     register int    num;
  425.  
  426.     if (strp == NULL || !isdigit(*strp))
  427.         return NULL;
  428.     num = 0;
  429.     while ((c = *strp) != '\0' && isdigit(c)) {
  430.         num = num * 10 + (c - '0');
  431.         if (num > max)
  432.             return NULL;    /* illegal value */
  433.         ++strp;
  434.     }
  435.     if (num < min)
  436.         return NULL;        /* illegal value */
  437.     *nump = num;
  438.     return strp;
  439. }
  440.  
  441. /*
  442. ** Given a pointer into a time zone string, extract a number of seconds,
  443. ** in hh[:mm[:ss]] form, from the string.
  444. ** If any error occurs, return NULL.
  445. ** Otherwise, return a pointer to the first character not part of the number
  446. ** of seconds.
  447. */
  448.  
  449. static const char *
  450. getsecs(strp, secsp)
  451. register const char *    strp;
  452. long * const        secsp;
  453. {
  454.     int    num;
  455.  
  456.     strp = getnum(strp, &num, 0, HOURSPERDAY);
  457.     if (strp == NULL)
  458.         return NULL;
  459.     *secsp = num * SECSPERHOUR;
  460.     if (*strp == ':') {
  461.         ++strp;
  462.         strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
  463.         if (strp == NULL)
  464.             return NULL;
  465.         *secsp += num * SECSPERMIN;
  466.         if (*strp == ':') {
  467.             ++strp;
  468.             strp = getnum(strp, &num, 0, SECSPERMIN - 1);
  469.             if (strp == NULL)
  470.                 return NULL;
  471.             *secsp += num;
  472.         }
  473.     }
  474.     return strp;
  475. }
  476.  
  477. /*
  478. ** Given a pointer into a time zone string, extract an offset, in
  479. ** [+-]hh[:mm[:ss]] form, from the string.
  480. ** If any error occurs, return NULL.
  481. ** Otherwise, return a pointer to the first character not part of the time.
  482. */
  483.  
  484. static const char *
  485. getoffset(strp, offsetp)
  486. register const char *    strp;
  487. long * const        offsetp;
  488. {
  489.     register int    neg;
  490.  
  491.     if (*strp == '-') {
  492.         neg = 1;
  493.         ++strp;
  494.     } else if (isdigit(*strp) || *strp++ == '+')
  495.         neg = 0;
  496.     else    return NULL;        /* illegal offset */
  497.     strp = getsecs(strp, offsetp);
  498.     if (strp == NULL)
  499.         return NULL;        /* illegal time */
  500.     if (neg)
  501.         *offsetp = -*offsetp;
  502.     return strp;
  503. }
  504.  
  505. /*
  506. ** Given a pointer into a time zone string, extract a rule in the form
  507. ** date[/time].  See POSIX section 8 for the format of "date" and "time".
  508. ** If a valid rule is not found, return NULL.
  509. ** Otherwise, return a pointer to the first character not part of the rule.
  510. */
  511.  
  512. static const char *
  513. getrule(strp, rulep)
  514. const char *            strp;
  515. register struct rule * const    rulep;
  516. {
  517.     if (*strp == 'J') {
  518.         /*
  519.         ** Julian day.
  520.         */
  521.         rulep->r_type = JULIAN_DAY;
  522.         ++strp;
  523.         strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
  524.     } else if (*strp == 'M') {
  525.         /*
  526.         ** Month, week, day.
  527.         */
  528.         rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
  529.         ++strp;
  530.         strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
  531.         if (strp == NULL)
  532.             return NULL;
  533.         if (*strp++ != '.')
  534.             return NULL;
  535.         strp = getnum(strp, &rulep->r_week, 1, 5);
  536.         if (strp == NULL)
  537.             return NULL;
  538.         if (*strp++ != '.')
  539.             return NULL;
  540.         strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
  541.     } else if (isdigit(*strp)) {
  542.         /*
  543.         ** Day of year.
  544.         */
  545.         rulep->r_type = DAY_OF_YEAR;
  546.         strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
  547.     } else    return NULL;        /* invalid format */
  548.     if (strp == NULL)
  549.         return NULL;
  550.     if (*strp == '/') {
  551.         /*
  552.         ** Time specified.
  553.         */
  554.         ++strp;
  555.         strp = getsecs(strp, &rulep->r_time);
  556.     } else    rulep->r_time = 2 * SECSPERHOUR;    /* default = 2:00:00 */
  557.     return strp;
  558. }
  559.  
  560. /*
  561. ** Given the Epoch-relative time of January 1, 00:00:00 GMT, in a year, the
  562. ** year, a rule, and the offset from GMT at the time that rule takes effect,
  563. ** calculate the Epoch-relative time that rule takes effect.
  564. */
  565.  
  566. static time_t
  567. transtime(janfirst, year, rulep, offset)
  568. const time_t                janfirst;
  569. const int                year;
  570. register const struct rule * const    rulep;
  571. const long                offset;
  572. {
  573.     register int    leapyear;
  574.     register time_t    value;
  575.     register int    i;
  576.     int        d, m1, yy0, yy1, yy2, dow;
  577.  
  578.     leapyear = isleap(year);
  579.     switch (rulep->r_type) {
  580.  
  581.     case JULIAN_DAY:
  582.         /*
  583.         ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
  584.         ** years.
  585.         ** In non-leap years, or if the day number is 59 or less, just
  586.         ** add SECSPERDAY times the day number-1 to the time of
  587.         ** January 1, midnight, to get the day.
  588.         */
  589.         value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
  590.         if (leapyear && rulep->r_day >= 60)
  591.             value += SECSPERDAY;
  592.         break;
  593.  
  594.     case DAY_OF_YEAR:
  595.         /*
  596.         ** n - day of year.
  597.         ** Just add SECSPERDAY times the day number to the time of
  598.         ** January 1, midnight, to get the day.
  599.         */
  600.         value = janfirst + rulep->r_day * SECSPERDAY;
  601.         break;
  602.  
  603.     case MONTH_NTH_DAY_OF_WEEK:
  604.         /*
  605.         ** Mm.n.d - nth "dth day" of month m.
  606.         */
  607.         value = janfirst;
  608.         for (i = 0; i < rulep->r_mon - 1; ++i)
  609.             value += mon_lengths[leapyear][i] * SECSPERDAY;
  610.  
  611.         /*
  612.         ** Use Zeller's Congruence to get day-of-week of first day of
  613.         ** month.
  614.         */
  615.         m1 = (rulep->r_mon + 9) % 12 + 1;
  616.         yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
  617.         yy1 = yy0 / 100;
  618.         yy2 = yy0 % 100;
  619.         dow = ((26 * m1 - 2) / 10 +
  620.             1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
  621.         if (dow < 0)
  622.             dow += DAYSPERWEEK;
  623.  
  624.         /*
  625.         ** "dow" is the day-of-week of the first day of the month.  Get
  626.         ** the day-of-month (zero-origin) of the first "dow" day of the
  627.         ** month.
  628.         */
  629.         d = rulep->r_day - dow;
  630.         if (d < 0)
  631.             d += DAYSPERWEEK;
  632.         for (i = 1; i < rulep->r_week; ++i) {
  633.             if (d + DAYSPERWEEK >=
  634.                 mon_lengths[leapyear][rulep->r_mon - 1])
  635.                     break;
  636.             d += DAYSPERWEEK;
  637.         }
  638.  
  639.         /*
  640.         ** "d" is the day-of-month (zero-origin) of the day we want.
  641.         */
  642.         value += d * SECSPERDAY;
  643.         break;
  644.     }
  645.  
  646.     /*
  647.     ** "value" is the Epoch-relative time of 00:00:00 GMT on the day in
  648.     ** question.  To get the Epoch-relative time of the specified local
  649.     ** time on that day, add the transition time and the current offset
  650.     ** from GMT.
  651.     */
  652.     return value + rulep->r_time + offset;
  653. }
  654.  
  655. /*
  656. ** Given a POSIX section 8-style TZ string, fill in the rule tables as
  657. ** appropriate.
  658. */
  659.  
  660. static int
  661. tzparse(name, sp, lastditch)
  662. const char *            name;
  663. register struct state * const    sp;
  664. const int            lastditch;
  665. {
  666.     const char *            stdname;
  667.     const char *            dstname;
  668.     int                stdlen;
  669.     int                dstlen;
  670.     long                stdoffset;
  671.     long                dstoffset;
  672.     register time_t *        atp;
  673.     register unsigned char *    typep;
  674.     register char *            cp;
  675.     register int            load_result;
  676.  
  677.     stdname = name;
  678.     if (lastditch) {
  679.         stdlen = strlen(name);    /* length of standard zone name */
  680.         name += stdlen;
  681.         if (stdlen >= sizeof sp->chars)
  682.             stdlen = (sizeof sp->chars) - 1;
  683.     } else {
  684.         name = getzname(name);
  685.         stdlen = name - stdname;
  686.         if (stdlen < 3)
  687.             return -1;
  688.     }
  689.     if (*name == '\0')
  690.         return -1;
  691.     else {
  692.         name = getoffset(name, &stdoffset);
  693.         if (name == NULL)
  694.             return -1;
  695.     }
  696.     load_result = tzload(TZDEFRULES, sp);
  697.     if (load_result != 0)
  698.         sp->leapcnt = 0;        /* so, we're off a little */
  699.     if (*name != '\0') {
  700.         dstname = name;
  701.         name = getzname(name);
  702.         dstlen = name - dstname;    /* length of DST zone name */
  703.         if (dstlen < 3)
  704.             return -1;
  705.         if (*name != '\0' && *name != ',' && *name != ';') {
  706.             name = getoffset(name, &dstoffset);
  707.             if (name == NULL)
  708.                 return -1;
  709.         } else    dstoffset = stdoffset - SECSPERHOUR;
  710.         if (*name == ',' || *name == ';') {
  711.             struct rule    start;
  712.             struct rule    end;
  713.             register int    year;
  714.             register time_t    janfirst;
  715.             time_t        starttime;
  716.             time_t        endtime;
  717.  
  718.             ++name;
  719.             if ((name = getrule(name, &start)) == NULL)
  720.                 return -1;
  721.             if (*name++ != ',')
  722.                 return -1;
  723.             if ((name = getrule(name, &end)) == NULL)
  724.                 return -1;
  725.             if (*name != '\0')
  726.                 return -1;
  727.             sp->typecnt = 2;    /* standard time and DST */
  728.             /*
  729.             ** Two transitions per year, from EPOCH_YEAR to 2037.
  730.             */
  731.             sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
  732.             if (sp->timecnt > TZ_MAX_TIMES)
  733.                 return -1;
  734.             sp->ttis[0].tt_gmtoff = -dstoffset;
  735.             sp->ttis[0].tt_isdst = 1;
  736.             sp->ttis[0].tt_abbrind = stdlen + 1;
  737.             sp->ttis[1].tt_gmtoff = -stdoffset;
  738.             sp->ttis[1].tt_isdst = 0;
  739.             sp->ttis[1].tt_abbrind = 0;
  740.             atp = sp->ats;
  741.             typep = sp->types;
  742.             janfirst = 0;
  743.             for (year = EPOCH_YEAR; year <= 2037; ++year) {
  744.                 starttime = transtime(janfirst, year, &start,
  745.                     stdoffset);
  746.                 endtime = transtime(janfirst, year, &end,
  747.                     dstoffset);
  748.                 if (starttime > endtime) {
  749.                     *atp++ = endtime;
  750.                     *typep++ = 1;    /* DST ends */
  751.                     *atp++ = starttime;
  752.                     *typep++ = 0;    /* DST begins */
  753.                 } else {
  754.                     *atp++ = starttime;
  755.                     *typep++ = 0;    /* DST begins */
  756.                     *atp++ = endtime;
  757.                     *typep++ = 1;    /* DST ends */
  758.                 }
  759.                 janfirst +=
  760.                     year_lengths[isleap(year)] * SECSPERDAY;
  761.             }
  762.         } else {
  763.             int        sawstd;
  764.             int        sawdst;
  765.             long        stdfix;
  766.             long        dstfix;
  767.             long        oldfix;
  768.             int        isdst;
  769.             register int    i;
  770.  
  771.             if (*name != '\0')
  772.                 return -1;
  773.             if (load_result != 0)
  774.                 return -1;
  775.             /*
  776.             ** Compute the difference between the real and
  777.             ** prototype standard and summer time offsets
  778.             ** from GMT, and put the real standard and summer
  779.             ** time offsets into the rules in place of the
  780.             ** prototype offsets.
  781.             */
  782.             sawstd = FALSE;
  783.             sawdst = FALSE;
  784.             stdfix = 0;
  785.             dstfix = 0;
  786.             for (i = 0; i < sp->typecnt; ++i) {
  787.                 if (sp->ttis[i].tt_isdst) {
  788.                     oldfix = dstfix;
  789.                     dstfix =
  790.                         sp->ttis[i].tt_gmtoff + dstoffset;
  791.                     if (sawdst && (oldfix != dstfix))
  792.                         return -1;
  793.                     sp->ttis[i].tt_gmtoff = -dstoffset;
  794.                     sp->ttis[i].tt_abbrind = stdlen + 1;
  795.                     sawdst = TRUE;
  796.                 } else {
  797.                     oldfix = stdfix;
  798.                     stdfix =
  799.                         sp->ttis[i].tt_gmtoff + stdoffset;
  800.                     if (sawstd && (oldfix != stdfix))
  801.                         return -1;
  802.                     sp->ttis[i].tt_gmtoff = -stdoffset;
  803.                     sp->ttis[i].tt_abbrind = 0;
  804.                     sawstd = TRUE;
  805.                 }
  806.             }
  807.             /*
  808.             ** Make sure we have both standard and summer time.
  809.             */
  810.             if (!sawdst || !sawstd)
  811.                 return -1;
  812.             /*
  813.             ** Now correct the transition times by shifting
  814.             ** them by the difference between the real and
  815.             ** prototype offsets.  Note that this difference
  816.             ** can be different in standard and summer time;
  817.             ** the prototype probably has a 1-hour difference
  818.             ** between standard and summer time, but a different
  819.             ** difference can be specified in TZ.
  820.             */
  821.             isdst = FALSE;    /* we start in standard time */
  822.             for (i = 0; i < sp->timecnt; ++i) {
  823.                 register const struct ttinfo *    ttisp;
  824.  
  825.                 /*
  826.                 ** If summer time is in effect, and the
  827.                 ** transition time was not specified as
  828.                 ** standard time, add the summer time
  829.                 ** offset to the transition time;
  830.                 ** otherwise, add the standard time offset
  831.                 ** to the transition time.
  832.                 */
  833.                 ttisp = &sp->ttis[sp->types[i]];
  834.                 sp->ats[i] +=
  835.                     (isdst && !ttisp->tt_ttisstd) ?
  836.                         dstfix : stdfix;
  837.                 isdst = ttisp->tt_isdst;
  838.             }
  839.         }
  840.     } else {
  841.         dstlen = 0;
  842.         sp->typecnt = 1;        /* only standard time */
  843.         sp->timecnt = 0;
  844.         sp->ttis[0].tt_gmtoff = -stdoffset;
  845.         sp->ttis[0].tt_isdst = 0;
  846.         sp->ttis[0].tt_abbrind = 0;
  847.     }
  848.     sp->charcnt = stdlen + 1;
  849.     if (dstlen != 0)
  850.         sp->charcnt += dstlen + 1;
  851.     if (sp->charcnt > sizeof sp->chars)
  852.         return -1;
  853.     cp = sp->chars;
  854.     (void) strncpy(cp, stdname, stdlen);
  855.     cp += stdlen;
  856.     *cp++ = '\0';
  857.     if (dstlen != 0) {
  858.         (void) strncpy(cp, dstname, dstlen);
  859.         *(cp + dstlen) = '\0';
  860.     }
  861.     return 0;
  862. }
  863.  
  864. static void
  865. gmtload(sp)
  866. struct state * const    sp;
  867. {
  868.     if (tzload(GMT, sp) != 0)
  869.         (void) tzparse(GMT, sp, TRUE);
  870. }
  871.  
  872. void
  873. tzset()
  874. {
  875.     register const char *    name;
  876.     void tzsetwall();
  877.  
  878.     name = getenv("TZ");
  879.     if (name == NULL) {
  880. #if 0
  881.         tzsetwall();
  882.         return;
  883. #else
  884.         name = "EST5";
  885. #endif
  886.     }
  887.     lcl_is_set = TRUE;
  888. #ifdef ALL_STATE
  889.     if (lclptr == NULL) {
  890.         lclptr = (struct state *) malloc(sizeof *lclptr);
  891.         if (lclptr == NULL) {
  892.             settzname();    /* all we can do */
  893.             return;
  894.         }
  895.     }
  896. #endif /* defined ALL_STATE */
  897.     if (*name == '\0') {
  898.         /*
  899.         ** User wants it fast rather than right.
  900.         */
  901.         lclptr->leapcnt = 0;        /* so, we're off a little */
  902.         lclptr->timecnt = 0;
  903.         lclptr->ttis[0].tt_gmtoff = 0;
  904.         lclptr->ttis[0].tt_abbrind = 0;
  905.         (void) strcpy(lclptr->chars, GMT);
  906.     } else if (tzload(name, lclptr) != 0)
  907.         if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
  908.             (void) gmtload(lclptr);
  909.     settzname();
  910. }
  911.  
  912. void
  913. tzsetwall()
  914. {
  915.     lcl_is_set = TRUE;
  916. #ifdef ALL_STATE
  917.     if (lclptr == NULL) {
  918.         lclptr = (struct state *) malloc(sizeof *lclptr);
  919.         if (lclptr == NULL) {
  920.             settzname();    /* all we can do */
  921.             return;
  922.         }
  923.     }
  924. #endif /* defined ALL_STATE */
  925.     if (tzload((char *) NULL, lclptr) != 0)
  926.         gmtload(lclptr);
  927.     settzname();
  928. }
  929.  
  930. /*
  931. ** The easy way to behave "as if no library function calls" localtime
  932. ** is to not call it--so we drop its guts into "localsub", which can be
  933. ** freely called.  (And no, the PANS doesn't require the above behavior--
  934. ** but it *is* desirable.)
  935. **
  936. ** The unused offset argument is for the benefit of mktime variants.
  937. */
  938.  
  939. /*ARGSUSED*/
  940. static void
  941. localsub(timep, offset, tmp)
  942. const time_t * const    timep;
  943. const long        offset;
  944. struct tm * const    tmp;
  945. {
  946.     register const struct state *    sp;
  947.     register const struct ttinfo *    ttisp;
  948.     register int            i;
  949.     const time_t            t = *timep;
  950.  
  951.     if (!lcl_is_set)
  952.         tzset();
  953.     sp = lclptr;
  954. #ifdef ALL_STATE
  955.     if (sp == NULL) {
  956.         gmtsub(timep, offset, tmp);
  957.         return;
  958.     }
  959. #endif /* defined ALL_STATE */
  960.     if (sp->timecnt == 0 || t < sp->ats[0]) {
  961.         i = 0;
  962.         while (sp->ttis[i].tt_isdst)
  963.             if (++i >= sp->typecnt) {
  964.                 i = 0;
  965.                 break;
  966.             }
  967.     } else {
  968.         for (i = 1; i < sp->timecnt; ++i)
  969.             if (t < sp->ats[i])
  970.                 break;
  971.         i = sp->types[i - 1];
  972.     }
  973.     ttisp = &sp->ttis[i];
  974.     /*
  975.     ** To get (wrong) behavior that's compatible with System V Release 2.0
  976.     ** you'd replace the statement below with
  977.     **    t += ttisp->tt_gmtoff;
  978.     **    timesub(&t, 0L, sp, tmp);
  979.     */
  980.     timesub(&t, ttisp->tt_gmtoff, sp, tmp);
  981.     tmp->tm_isdst = ttisp->tt_isdst;
  982.     tzname[tmp->tm_isdst] = (char *) &sp->chars[ttisp->tt_abbrind];
  983.     tmp->tm_zone = &sp->chars[ttisp->tt_abbrind];
  984. }
  985.  
  986. struct tm *
  987. localtime(timep)
  988. const time_t * const    timep;
  989. {
  990.     static struct tm    tm;
  991.  
  992.     localsub(timep, 0L, &tm);
  993.     return &tm;
  994. }
  995.  
  996. /*
  997. ** gmtsub is to gmtime as localsub is to localtime.
  998. */
  999.  
  1000. static void
  1001. gmtsub(timep, offset, tmp)
  1002. const time_t * const    timep;
  1003. const long        offset;
  1004. struct tm * const    tmp;
  1005. {
  1006.     if (!gmt_is_set) {
  1007.         gmt_is_set = TRUE;
  1008. #ifdef ALL_STATE
  1009.         gmtptr = (struct state *) malloc(sizeof *gmtptr);
  1010.         if (gmtptr != NULL)
  1011. #endif /* defined ALL_STATE */
  1012.             gmtload(gmtptr);
  1013.     }
  1014.     timesub(timep, offset, gmtptr, tmp);
  1015.     /*
  1016.     ** Could get fancy here and deliver something such as
  1017.     ** "GMT+xxxx" or "GMT-xxxx" if offset is non-zero,
  1018.     ** but this is no time for a treasure hunt.
  1019.     */
  1020.     if (offset != 0)
  1021.         tmp->tm_zone = WILDABBR;
  1022.     else {
  1023. #ifdef ALL_STATE
  1024.         if (gmtptr == NULL)
  1025.             tmp->TM_ZONE = GMT;
  1026.         else    tmp->TM_ZONE = gmtptr->chars;
  1027. #endif /* defined ALL_STATE */
  1028. #ifndef ALL_STATE
  1029.         tmp->tm_zone = gmtptr->chars;
  1030. #endif /* State Farm */
  1031.     }
  1032. }
  1033.  
  1034. struct tm *
  1035. gmtime(timep)
  1036. const time_t * const    timep;
  1037. {
  1038.     static struct tm    tm;
  1039.  
  1040.     gmtsub(timep, 0L, &tm);
  1041.     return &tm;
  1042. }
  1043.  
  1044. static void
  1045. timesub(timep, offset, sp, tmp)
  1046. const time_t * const            timep;
  1047. const long                offset;
  1048. register const struct state * const    sp;
  1049. register struct tm * const        tmp;
  1050. {
  1051.     register const struct lsinfo *    lp;
  1052.     register long            days;
  1053.     register long            rem;
  1054.     register int            y;
  1055.     register int            yleap;
  1056.     register const int *        ip;
  1057.     register long            corr;
  1058.     register int            hit;
  1059.     register int            i;
  1060.  
  1061.     corr = 0;
  1062.     hit = FALSE;
  1063. #ifdef ALL_STATE
  1064.     i = (sp == NULL) ? 0 : sp->leapcnt;
  1065. #endif /* defined ALL_STATE */
  1066. #ifndef ALL_STATE
  1067.     i = sp->leapcnt;
  1068. #endif /* State Farm */
  1069.     while (--i >= 0) {
  1070.         lp = &sp->lsis[i];
  1071.         if (*timep >= lp->ls_trans) {
  1072.             if (*timep == lp->ls_trans)
  1073.                 hit = ((i == 0 && lp->ls_corr > 0) ||
  1074.                     lp->ls_corr > sp->lsis[i - 1].ls_corr);
  1075.             corr = lp->ls_corr;
  1076.             break;
  1077.         }
  1078.     }
  1079.     days = *timep / SECSPERDAY;
  1080.     rem = *timep % SECSPERDAY;
  1081. #ifdef mc68k
  1082.     if (*timep == 0x80000000) {
  1083.         /*
  1084.         ** A 3B1 muffs the division on the most negative number.
  1085.         */
  1086.         days = -24855;
  1087.         rem = -11648;
  1088.     }
  1089. #endif /* mc68k */
  1090.     rem += (offset - corr);
  1091.     while (rem < 0) {
  1092.         rem += SECSPERDAY;
  1093.         --days;
  1094.     }
  1095.     while (rem >= SECSPERDAY) {
  1096.         rem -= SECSPERDAY;
  1097.         ++days;
  1098.     }
  1099.     tmp->tm_hour = (int) (rem / SECSPERHOUR);
  1100.     rem = rem % SECSPERHOUR;
  1101.     tmp->tm_min = (int) (rem / SECSPERMIN);
  1102.     tmp->tm_sec = (int) (rem % SECSPERMIN);
  1103.     if (hit)
  1104.         /*
  1105.         ** A positive leap second requires a special
  1106.         ** representation.  This uses "... ??:59:60".
  1107.         */
  1108.         ++(tmp->tm_sec);
  1109.     tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
  1110.     if (tmp->tm_wday < 0)
  1111.         tmp->tm_wday += DAYSPERWEEK;
  1112.     y = EPOCH_YEAR;
  1113.     if (days >= 0)
  1114.         for ( ; ; ) {
  1115.             yleap = isleap(y);
  1116.             if (days < (long) year_lengths[yleap])
  1117.                 break;
  1118.             ++y;
  1119.             days = days - (long) year_lengths[yleap];
  1120.         }
  1121.     else do {
  1122.         --y;
  1123.         yleap = isleap(y);
  1124.         days = days + (long) year_lengths[yleap];
  1125.     } while (days < 0);
  1126.     tmp->tm_year = y - TM_YEAR_BASE;
  1127.     tmp->tm_yday = (int) days;
  1128.     ip = mon_lengths[yleap];
  1129.     for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
  1130.         days = days - (long) ip[tmp->tm_mon];
  1131.     tmp->tm_mday = (int) (days + 1);
  1132.     tmp->tm_isdst = 0;
  1133.     tmp->tm_gmtoff = offset;
  1134. }
  1135.  
  1136. /*
  1137. ** A la X3J11
  1138. */
  1139.  
  1140. char *
  1141. asctime(timeptr)
  1142. register const struct tm *    timeptr;
  1143. {
  1144.     static const char    wday_name[DAYSPERWEEK][3] = {
  1145.         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  1146.     };
  1147.     static const char    mon_name[MONSPERYEAR][3] = {
  1148.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  1149.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  1150.     };
  1151.     static char    result[26];
  1152.  
  1153.     (void) sprintf(result, "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n",
  1154.         wday_name[timeptr->tm_wday],
  1155.         mon_name[timeptr->tm_mon],
  1156.         timeptr->tm_mday, timeptr->tm_hour,
  1157.         timeptr->tm_min, timeptr->tm_sec,
  1158.         TM_YEAR_BASE + timeptr->tm_year);
  1159.     return result;
  1160. }
  1161.  
  1162. char *
  1163. ctime(timep)
  1164. const time_t * const    timep;
  1165. {
  1166.     return asctime(localtime(timep));
  1167. }
  1168.  
  1169. /*
  1170. ** Adapted from code provided by Robert Elz, who writes:
  1171. **    The "best" way to do mktime I think is based on an idea of Bob
  1172. **    Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
  1173. **    It does a binary search of the time_t space.  Since time_t's are
  1174. **    just 32 bits, its a max of 32 iterations (even at 64 bits it
  1175. **    would still be very reasonable).
  1176. */
  1177.  
  1178. #ifndef WRONG
  1179. #define WRONG    (-1)
  1180. #endif /* !defined WRONG */
  1181.  
  1182. static void
  1183. normalize(tensptr, unitsptr, base)
  1184. int * const    tensptr;
  1185. int * const    unitsptr;
  1186. const int    base;
  1187. {
  1188.     if (*unitsptr >= base) {
  1189.         *tensptr += *unitsptr / base;
  1190.         *unitsptr %= base;
  1191.     } else if (*unitsptr < 0) {
  1192.         --*tensptr;
  1193.         *unitsptr += base;
  1194.         if (*unitsptr < 0) {
  1195.             *tensptr -= 1 + (-*unitsptr) / base;
  1196.             *unitsptr = base - (-*unitsptr) % base;
  1197.         }
  1198.     }
  1199. }
  1200.  
  1201. static int
  1202. tmcomp(atmp, btmp)
  1203. register const struct tm * const atmp;
  1204. register const struct tm * const btmp;
  1205. {
  1206.     register int    result;
  1207.  
  1208.     if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
  1209.         (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
  1210.         (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
  1211.         (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
  1212.         (result = (atmp->tm_min - btmp->tm_min)) == 0)
  1213.             result = atmp->tm_sec - btmp->tm_sec;
  1214.     return result;
  1215. }
  1216.  
  1217. static time_t
  1218. time2(tmp, funcp, offset, okayp)
  1219. struct tm * const    tmp;
  1220. void (* const        funcp)();
  1221. const long        offset;
  1222. int * const        okayp;
  1223. {
  1224.     register const struct state *    sp;
  1225.     register int            dir;
  1226.     register int            bits;
  1227.     register int            i, j ;
  1228.     register int            saved_seconds;
  1229.     time_t                newt;
  1230.     time_t                t;
  1231.     struct tm            yourtm, mytm;
  1232.  
  1233.     *okayp = FALSE;
  1234.     yourtm = *tmp;
  1235.     if (yourtm.tm_sec >= SECSPERMIN + 2 || yourtm.tm_sec < 0)
  1236.         normalize(&yourtm.tm_min, &yourtm.tm_sec, SECSPERMIN);
  1237.     normalize(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR);
  1238.     normalize(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY);
  1239.     normalize(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR);
  1240.     while (yourtm.tm_mday <= 0) {
  1241.         --yourtm.tm_year;
  1242.         yourtm.tm_mday +=
  1243.             year_lengths[isleap(yourtm.tm_year + TM_YEAR_BASE)];
  1244.     }
  1245.     for ( ; ; ) {
  1246.         i = mon_lengths[isleap(yourtm.tm_year +
  1247.             TM_YEAR_BASE)][yourtm.tm_mon];
  1248.         if (yourtm.tm_mday <= i)
  1249.             break;
  1250.         yourtm.tm_mday -= i;
  1251.         if (++yourtm.tm_mon >= MONSPERYEAR) {
  1252.             yourtm.tm_mon = 0;
  1253.             ++yourtm.tm_year;
  1254.         }
  1255.     }
  1256.     saved_seconds = yourtm.tm_sec;
  1257.     yourtm.tm_sec = 0;
  1258.     /*
  1259.     ** Calculate the number of magnitude bits in a time_t
  1260.     ** (this works regardless of whether time_t is
  1261.     ** signed or unsigned, though lint complains if unsigned).
  1262.     */
  1263.     for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
  1264.         ;
  1265.     /*
  1266.     ** If time_t is signed, then 0 is the median value,
  1267.     ** if time_t is unsigned, then 1 << bits is median.
  1268.     */
  1269.     t = (t < 0) ? 0 : ((time_t) 1 << bits);
  1270.     for ( ; ; ) {
  1271.         (*funcp)(&t, offset, &mytm);
  1272.         dir = tmcomp(&mytm, &yourtm);
  1273.         if (dir != 0) {
  1274.             if (bits-- < 0)
  1275.                 return WRONG;
  1276.             if (bits < 0)
  1277.                 --t;
  1278.             else if (dir > 0)
  1279.                 t -= (time_t) 1 << bits;
  1280.             else    t += (time_t) 1 << bits;
  1281.             continue;
  1282.         }
  1283.         if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
  1284.             break;
  1285.         /*
  1286.         ** Right time, wrong type.
  1287.         ** Hunt for right time, right type.
  1288.         ** It's okay to guess wrong since the guess
  1289.         ** gets checked.
  1290.         */
  1291.         sp = (const struct state *)
  1292.             ((funcp == localsub) ? lclptr : gmtptr);
  1293. #ifdef ALL_STATE
  1294.         if (sp == NULL)
  1295.             return WRONG;
  1296. #endif /* defined ALL_STATE */
  1297.         for (i = 0; i < sp->typecnt; ++i) {
  1298.             if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
  1299.                 continue;
  1300.             for (j = 0; j < sp->typecnt; ++j) {
  1301.                 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
  1302.                     continue;
  1303.                 newt = t + sp->ttis[j].tt_gmtoff -
  1304.                     sp->ttis[i].tt_gmtoff;
  1305.                 (*funcp)(&newt, offset, &mytm);
  1306.                 if (tmcomp(&mytm, &yourtm) != 0)
  1307.                     continue;
  1308.                 if (mytm.tm_isdst != yourtm.tm_isdst)
  1309.                     continue;
  1310.                 /*
  1311.                 ** We have a match.
  1312.                 */
  1313.                 t = newt;
  1314.                 goto label;
  1315.             }
  1316.         }
  1317.         return WRONG;
  1318.     }
  1319. label:
  1320.     t += saved_seconds;
  1321.     (*funcp)(&t, offset, tmp);
  1322.     *okayp = TRUE;
  1323.     return t;
  1324. }
  1325.  
  1326. static time_t
  1327. time1(tmp, funcp, offset)
  1328. struct tm * const    tmp;
  1329. void (* const        funcp)();
  1330. const long        offset;
  1331. {
  1332.     register time_t            t;
  1333.     register const struct state *    sp;
  1334.     register int            samei, otheri;
  1335.     int                okay;
  1336.  
  1337.     if (tmp->tm_isdst > 1)
  1338.         tmp->tm_isdst = 1;
  1339.     t = time2(tmp, funcp, offset, &okay);
  1340.     if (okay || tmp->tm_isdst < 0)
  1341.         return t;
  1342.     /*
  1343.     ** We're supposed to assume that somebody took a time of one type
  1344.     ** and did some math on it that yielded a "struct tm" that's bad.
  1345.     ** We try to divine the type they started from and adjust to the
  1346.     ** type they need.
  1347.     */
  1348.     sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
  1349. #ifdef ALL_STATE
  1350.     if (sp == NULL)
  1351.         return WRONG;
  1352. #endif /* defined ALL_STATE */
  1353.     for (samei = 0; samei < sp->typecnt; ++samei) {
  1354.         if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
  1355.             continue;
  1356.         for (otheri = 0; otheri < sp->typecnt; ++otheri) {
  1357.             if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
  1358.                 continue;
  1359.             tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
  1360.                     sp->ttis[samei].tt_gmtoff;
  1361.             tmp->tm_isdst = !tmp->tm_isdst;
  1362.             t = time2(tmp, funcp, offset, &okay);
  1363.             if (okay)
  1364.                 return t;
  1365.             tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
  1366.                     sp->ttis[samei].tt_gmtoff;
  1367.             tmp->tm_isdst = !tmp->tm_isdst;
  1368.         }
  1369.     }
  1370.     return WRONG;
  1371. }
  1372.  
  1373. time_t
  1374. mktime(tmp)
  1375. const struct tm * tmp;
  1376. {
  1377.     return time1(tmp, localsub, 0L);
  1378. }
  1379.